package com.caigou.modules.home.adapter

import android.app.Activity
import android.widget.TextView
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.caigou.comm.base.adapter.PrizeBallItemAdapter
import com.caigou.comm.base.bean.PrizeItemBean
import com.caigou.comm.base.ext.prizeBallColor
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.home.R
import com.caigou.modules.home.bean.PrizeBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * @author #Suyghur.
 * Created on 2021/12/19
 */
class HistoryPrizeItemAdapter(private val activity: Activity, dataset: MutableList<PrizeBean>) :
    BaseQuickAdapter<PrizeBean, BaseViewHolder>(R.layout.item_history_prize_list, dataset) {
    override fun convert(holder: BaseViewHolder, item: PrizeBean) {
        holder.setText(R.id.item_history_prize_tv_period, "第${item.period}期")
        holder.setText(R.id.item_history_prize_tv_time, item.createTime)
        val mAdapter = PrizeBallItemAdapter(mutableListOf())
        holder.getView<RecyclerView>(R.id.item_history_prize_recycler_view).run {
            layoutManager = GridLayoutManager(activity, 6)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(activity))
        }
        if (item.prizeList.size != 7) {
            item.prizeList = mutableListOf(
                PrizeItemBean(1, "鼠", "00", 1, 0),
                PrizeItemBean(1, "鼠", "00", 1, 0),
                PrizeItemBean(1, "鼠", "00", 1, 0),
                PrizeItemBean(1, "鼠", "00", 1, 0),
                PrizeItemBean(1, "鼠", "00", 1, 0),
                PrizeItemBean(1, "鼠", "00", 1, 0),
                PrizeItemBean(1, "鼠", "00", 1, 0)
            )
        }
        val balls = item.prizeList.slice(0..item.prizeList.size - 2)
        mAdapter.setList(balls)
        val lastBall = item.prizeList[item.prizeList.size - 1]
        with(holder.getView<TextView>(R.id.item_history_prize_tv_num)) {
            text = lastBall.number
            setBackgroundResource(prizeBallColor(lastBall.numberColor))
        }
        holder.setText(R.id.item_history_prize_tv_desc, lastBall.animalName)
    }
}